package com.example.eeuse.controller;


import com.example.eeuse.ReqCall;
import com.example.eeuse.mapper.DocumentMapper;
import com.example.eeuse.mapper.LogUpdateCacheMapper;
import com.example.eeuse.model.Document;
import com.example.eeuse.model.LogUpdateCache;
import lombok.RequiredArgsConstructor;
import org.apache.http.client.utils.DateUtils;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * 测试使用Easy-ES
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@RestController
public class TestUseEeController {
    @Autowired(required = false)
    private  DocumentMapper documentMapper;

    @Autowired(required = false)
    private  LogUpdateCacheMapper logUpdateCacheMapper;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return documentMapper.createIndex();
    }

    @GetMapping("/insert")
    public Integer insert() {
        // 2.初始化-> 新增数据
        Document document = new Document();
        document.setTitle("老汉");
        document.setContent("推*技术过硬");
        return documentMapper.insert(document);
    }

    @GetMapping("/search")
    public List<Document> search() {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, "老汉");
        return documentMapper.selectList(wrapper);
    }

    @GetMapping("/searchLog")
    public String searchLog() {
        long begin=System.currentTimeMillis();
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<LogUpdateCache> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.like(LogUpdateCache::getCacheKey, "CAN");
        wrapper.groupBy("cacheKey.keyword");
        //EsPageInfo<LogUpdateCache> page=logUpdateCacheMapper.pageQuery(wrapper,1,500);
        SearchResponse searchResponse=logUpdateCacheMapper.search(wrapper);
        System.out.println(System.currentTimeMillis()-begin);
        return "";
    }

    @GetMapping("/insertLog")
    public Integer insertLog() {
        // 2.初始化-> 新增数据
        LogUpdateCache document = new LogUpdateCache();
        document.setId("老汉");
       return logUpdateCacheMapper.insert(document,"index_log_cache_update-20230904");
    }

    @GetMapping("/testHold")
    public String testHold(@RequestParam("id")int id)
    {

        try {
           Future<String> future= threadPoolTaskExecutor.submit(new ReqCall(id));

           return future.get(15, TimeUnit.SECONDS);
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
            return "fail";
        }
    }

    @GetMapping("/showThread")
    public String showThread()
    {
       String msg=threadPoolTaskExecutor.getActiveCount()+threadPoolTaskExecutor.getPoolSize()+"";
       return msg;
    }

    @GetMapping("/detail")
    public String detail(@RequestParam("id")int id)
    {
        System.out.println(DateUtils.formatDate(new Date(),"hh:mm:ss")+" "+id);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "succ";
    }

}
